package com.darren.archive.service.impl;

import com.darren.archive.dao.SysDepartmentDao;
import com.darren.archive.entity.SysDepartment;
import com.darren.archive.service.SysDepartmentService;
import com.darren.framework.service.impl.GenericServiceImpl;
import com.darren.framework.utils.Constant;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * *********************************************
 * Copyright DARREN
 * All rights reserved.
 * Description: TODO(description)
 * HISTORY:
 * *********************************************
 *  Version		Date		Author	   		Desc
 *   v1.0       2018-04-11  Darren Zhou    Create
 *
 * *********************************************
 * </pre>
 */

@Service
public class SysDepartmentServiceImpl extends GenericServiceImpl<SysDepartment, Long> implements SysDepartmentService {

    @Resource
    private SysDepartmentDao sysDepartmentDao;

    @Override
//    @Cacheable(value = Constant.CACHE_SYS_DEPARTMENTS, key = "T(String).valueOf(#departmentName).concat('-tree-').concat('departmentNameLike')")
    public List<SysDepartment> getDepartmentTree(String departmentName) {
        List<SysDepartment> sysDepartments = sysDepartmentDao.getSysDepartmentByName(departmentName);
        if (sysDepartments == null || sysDepartments.size() == 0) {
            return new ArrayList<SysDepartment>();
        }
        return constructFunctionTree(sysDepartments, 0L);
    }


    @Override
//    @Cacheable(value = Constant.CACHE_SYS_DEPARTMENTS, key = "T(String).valueOf(#departmentName).concat('-list-').concat('departmentNameLike')")
    public List<SysDepartment> getDepartmentList(String departmentName) {
        List<SysDepartment> sysDepartments = sysDepartmentDao.getSysDepartmentByName(departmentName);
        return sysDepartments == null?new ArrayList<SysDepartment>():sysDepartments;
    }

    /**
     * 递归构造功能树结构
     *
     * @param departmentsList
     * @param parentId
     * @return
     */
    private List<SysDepartment> constructFunctionTree(List<SysDepartment> departmentsList, Long parentId) {
        List<SysDepartment> treeDepartments = departmentsList.stream().filter(item -> item.getParentId().equals(parentId)).collect(Collectors.toList());
        treeDepartments.sort((SysDepartment department1, SysDepartment department2) -> department1.getDisplaySequence().compareTo(department2.getDisplaySequence()));
        for (SysDepartment department : treeDepartments) {
            department.setChildren(constructFunctionTree(departmentsList, department.getId()));
        }
        return treeDepartments;
    }


    @Override
    @CacheEvict(value = Constant.CACHE_SYS_DEPARTMENTS, allEntries = true)
    public SysDepartment save(SysDepartment entity) {
        return sysDepartmentDao.merge(entity);
    }

    @Override
    @CacheEvict(value = Constant.CACHE_SYS_DEPARTMENTS, allEntries = true)
    public SysDepartment update(SysDepartment entity) {
        return sysDepartmentDao.update(entity);
    }

    @Override
    @CacheEvict(value = Constant.CACHE_SYS_DEPARTMENTS, allEntries = true)
    public void delete(SysDepartment entity) {
        sysDepartmentDao.delete(entity);
    }

    @Override
    @CacheEvict(value = Constant.CACHE_SYS_DEPARTMENTS, allEntries = true)
    public void deleteById(Long id) {
        sysDepartmentDao.deleteById(id);
    }

    @Override
    @Cacheable(value = Constant.CACHE_SYS_DEPARTMENTS, key = "#id")
    public SysDepartment findById(Long id) {
        return sysDepartmentDao.findById(id);
    }


}
